//
//  WmcSettingSwitchItemCell.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/21.
//

import UIKit

class WmcSettingSwitchItemCell: WmcSettingBaseItemCell {
    /// stackView
    private(set) lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 8.0
        stackV.distribution = .fill
        stackV.alignment = .fill
        return stackV
    }()
    /// switch开关
    private(set) lazy var switchBtn: UISwitch = {
        let switc = UISwitch()
        switc.onTintColor = .wmc_mainTheme
        return switc
    }()
    
    /// 红点
    private(set) lazy var redPointView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_red_F54A45
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 4
        return view
    }()
    
    let pointSize: CGFloat = 8.0
    
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        // 开关
        addSubview(switchBtn)
        switchBtn.snp.makeConstraints { make in
            make.right.equalTo(-Wmc_standardEdge.right.wmc_autoHor)
            make.centerY.equalToSuperview()
        }
        // 红点
        addSubview(redPointView)
        redPointView.snp.makeConstraints { make in
            make.top.right.equalTo(switchBtn)
            make.size.equalTo(pointSize)
        }
        // stackView
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(switchBtn.snp.left).offset(-8.0.wmc_autoHor)
            make.centerY.equalToSuperview()
        }
        // 添加标题
        stackView.addArrangedSubview(titleLabel)
    }
    
    
    // MARK: Bind Action
    
    override func bindEvent() {
        super.bindEvent()
        // 开关状态改变
        switchBtn.rx.value.changed.subscribe(onNext: {
            [weak self] _ in
            guard let self = self else { return }
            guard let switchItem = self.item as? WmcSettingSwitchItem else { return }
            switchItem.switchAction?(switchItem)
        }).disposed(by: rx.disposeBag)
    }
    
    override func didSetItem() {
        super.didSetItem()
        let switchItem = (item as? WmcSettingSwitchItem) ?? .init()
        switchBtn.isOn = switchItem.isOn
        redPointView.isHidden = !switchItem.isShowRedPoint
    }

}
